import axios from "axios";

const $axios = axios.create({
  baseURL: "/api",
  timeout: 3000,
  responseType: "json"
})
$axios.interceptors.request.use(config => {
  return config
}, error => {
  console.log(error)
})

$axios.interceptors.response.use(response => {
  return response.data
}, error => {
  console.log(error)
})

export const request = (url, params, headers, method = "POST") => {
  return new Promise((resolve, reject) => {
    $axios({
      method,
      url,
      data: params,
      headers: {
        "Content-Type": "application/json;charset=UTF-8",
        ...headers
      }
    }).then(result => {
      resolve(result)
    }).catch(error => {
      reject(error)
    })
  })
}